package stream;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.omg.CORBA.PUBLIC_MEMBER;

import java.time.LocalDate;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
public class UseStream {

    @Test
    public void changeArray2List(){
        String zipCodeData = "12,13,14,15,16,17";
        List<String> zipCodes = null;
        if(zipCodeData.contains(",")) {
            //zipCodes.addAll(Arrays.asList(zipCodeData.split(",")));
            zipCodes = new ArrayList<>(Arrays.asList(zipCodeData.split(","))); //重新构造下即可
        }
        System.out.println(zipCodes);
    }


    /**
     * 测试使用skip api批量插入
     *
     * 核心就是skip和limit的使用,挺不错的
     */
    @Test
    public void bulkInsert(){
        List<String> list = new ArrayList<>();
        int BULK_INSERT_COUNT = 5;
        for(int i = 1; i <= 71; i++){
            list.add(String.valueOf(i));
        }
        int loopCount = list.size() % BULK_INSERT_COUNT > 0 ? list.size() / BULK_INSERT_COUNT + 1 : list.size() / BULK_INSERT_COUNT;
        for ( int i = 0; i < loopCount; i++){
            List<String> collect = list.stream().skip(i * BULK_INSERT_COUNT).limit(BULK_INSERT_COUNT).collect(Collectors.toList());
            System.out.println(collect);
        }
    }

    @Test
    public void stream_skip() {
        List<Integer> collect = Stream.of(1, 2, 3, 4, 5, 6, 7, 8).skip(125).collect(Collectors.toList());
        System.out.println(collect);
    }

    /**
     * 对这个集合的流，做一系列的中间操作后，可以调用findFirst，返回集合的第一个对象
     * ifPresent有的话就执行,没有就不执行
     */
    @Test
    public void useFindFirst(){
        List<String> list = Arrays.asList("a","b","b","c","a");
        list.stream().filter(e -> Objects.equals(e,"b")).findAny().ifPresent(e->{
            try {
                Thread.sleep(5_000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            System.out.println(e);
        });
        System.out.println("outer");
        //和ifPresent里的代码是同步的,ifPresent里的代码不执行完外面的代码是不会执行的
    }


    /**
     * anyMatch表示，判断的条件里，任意一个元素成功，返回true
     * 如果是空的list,就返回false !!!
     *
     */
    @Test
    public void useAnyMatch(){
        List<String> list = Arrays.asList();
        System.out.println(list.size());//0
        boolean result = list.stream().anyMatch(e -> Objects.equals(e, "b"));//false
        System.out.println(result);
    }

    /**
     * Collectors.joining用于拼接
     * 不怕null
     */
    @Test
    public void useCollectorJoin(){
        List<String> list = Arrays.asList("a", "b", "c", "d", "", null);
        String result = list.stream().collect(Collectors.joining(">", "[", "]"));
        System.out.println(result);
    }


    /**
     * 用于合并代码 list1和list2
     */
    @Test
    public void useStreamContact(){
        List<String> list1 = Arrays.asList("a", "1", "c", "d");
        List<String> list2 = Arrays.asList("a", "b", "2", "d", "f");
        int size = Stream.concat(list1.stream(), list2.stream()).collect(Collectors.toList()).size();
        System.out.println(size);
    }

    @Test
    public void setValue(){
        List<Dish> list = new ArrayList<>();
        list.add(Dish.builder().id("1").name("one").build());
        list.add(Dish.builder().id("2").name("two").build());
        list.add(Dish.builder().id("2").name("two").build());

        String content = "1,2,3";
        StringBuffer sb = new StringBuffer();
        Arrays.stream(content.split(","))
                .forEach(e -> {
                    list.stream()
                            .filter(f -> e.equals(f.getId()))
                            .map(Dish::getName)
                            .findFirst()
                            .ifPresent(res->sb.append(res).append(","));
        });
        String s = sb.length() > 0 ? sb.substring(0, sb.length() - 1) : sb.toString();
        System.out.println(s);

    }

    /**
     * 实验证明 stream中使用filter直接进行true false的判断不会改变原数组,
     * 但是在filter里面给元素赋值了那就会改变原数组
     */
    @Test
    public void testFilter(){
        List<Dish> list  = Lists.newArrayList();
        Dish dish1 = new Dish();
        dish1.setId("1");
        Dish dish2 = new Dish();
        dish2.setId("2");
        list.add(dish1);
        list.add(dish2);
        List<Dish> collect = list.stream().filter(e -> {
            //e.setId("222222");
            return e.getId().equals("1");
        }).collect(Collectors.toList());
        System.out.println(list);
        System.out.println(collect);
    }

    /**
     * 实验证明 stream中使用foreach会改变原数组!!!
     */
    @Test
    public void testFilter2(){
        List<Dish> list  = Lists.newArrayList();
        Dish dish1 = new Dish();
        dish1.setId("1");
        Dish dish2 = new Dish();
        dish2.setId("2");
        list.add(dish1);
        list.add(dish2);
        list.stream().forEach(e->e.setId("222"));
        System.out.println(list);
    }

    @Test
    public void show2(){

        // ^\\s*(Z|z)\\s*(E|e).*

        String[] words = {"BT294BP",
                "SP118BG",
                "SO317AY",
                "HU177LX",
                "SP40JF",
                "OX253TQ",
                "YO124TJ",
                "ZE29EQ",
                "TR164FJ",
                "EX314AZ",
                "PE282EA",
                "L377AD",
                "PE86HB",
                "IP77RA",
                "PE228SD",
                "LN59NB",
                "LL653NY",
                "YO73DZ",
                "IV191PE",
                "NG235NG",
                "TR84HP",
                "CA87AT",
                "SY44DZ",
                "LN12ST",
                "RG291QT",
                "HA46NG",
                "NR128YB",
                "HG32SW",
                "PE339NP",
                "IV316SD",
                "CF624JD",
                "YO302AJ",
                "KY160JX",
                "DL79NJ",
                "NG323PZ",
                "DN214HZ",
                "IP311EE",
                "HS65DF",
                "PE129NJ",
                "OX106AA",
                "HP144UF",
                "SG166DN",
                "NE663JF",
                "HP225PG",
                "OX183LX",
                "YO187NT",
                "AB420NZ",
                "LS249SE",
                "LN44SY",
                "WV73EX",
                "NG348HB",
                "LN43LH",
                "YO187NT",
                "YO73DZ",
                "NE150RF",
                "GU240QQ",
                "SP97SL",
                "BH206JA",
                "DL93AQ",
                "SP97BA",
                "PO108DH",
                "BA129DT",
                "DL93JG",
                "NP167YG",
                "ST180AQ",
                "CO29NU",
                "SP97AS",
                "DT118RH",
                "DL93AD",
                "RG301HW",
                "ME44UG",
                "GU112BU",
                "GU111PS",
                "GU112PG",
                "DL93PZ",
                "IV23XE",
                "SP49DT",
                "CB102YA",
                "TW46HD",
                "SA626NN",
                "NG95HA",
                "HG42RD",
                "GU112BG",
                "TF93QE",
                "SL43DN",
                "SY38LZ",
                "CH24BD",
                "OX136JB",
                "GU166RN",
                "SP97DX",
                "RG189TP",
                "LD37RH",
                "EH139AL",
                "GU112DW",
                "YO325SB",
                "NE24NP",
                "IV27TD",
                "FK77RR",
                "PR28AA",
                "CV116QN",
                "DL94AT",
                "IP333RN",
                "GU179LP",
                "EH260NP",
                "CO27TR",
                "AB238DB",
                "GU112LF",
                "CT11SQ",
                "SW71SE",
                "GL31EZ",
                "YO104HD",
                "ME142NA",
                "SP97BQ",
                "LE157BQ",
                "IV363UH",
                "SP49JZ",
                "SP97BU",
                "CF143YE",
                "DL107NW",
                "GU112NG",
                "DL93QG",
                "CO27UT",
                "SP97EN",
                "HU177LX",
                "BT189QR",
                "SP49NY",
                "GU350JE",
                "NG317TE",
                "BN15AS",
                "YO325SW",
                "EH130PP",
                "NW14AN",
                "GU112AX",
                "SP48QU",
                "NR204QD",
                "IP123LZ",
                "G117DG",
                "CT203HG",
                "SO226NQ",
                "S102HU",
                "LE158RL",
                "FK81QZ",
                "SP119LQ",
                "LD37EA",
                "BT283NP",
                "GU112BX",
                "OX117ES",
                "SL41HF",
                "G208LQ",
                "SP49NA",
                "PR43JQ",
                "SW1E6HQ",
                "WS149PY",
                "EX26AR",
                "PL22BG",
                "PO13HL",
                "G848NX",
                "TR127RH",
                "BA228HT",
                "PO141AS",
                "TQ60HJ",
                "PO28ER",
                "PL112PD",
                "PO123BY",
                "PO12HB",
                "EX85AR",
                "PL13QS",
                "BH154NQ",
                "DD113SP",
                "TA26PF",
                "PL67AJ",
                "EX314AZ",
                "EX394LF",
                "HA46DQ",
                "HA46EP"
        };

        words = new String[]{
                "GX11_1AA",
        };


        String shu = "|";
        String content = "";

        for (String word:words) {

            String startPrefix = "^[\\\\s\\\\S]*";
            String endPrefix = "[\\\\s\\\\S]*$";

            String[] w = word.split("");
            content+=startPrefix;

            for (int i=0; i<w.length;i++) {
                String temp = "";
                if(i == w.length-1){
                    if(Character.isDigit(w[i].charAt(0))){
                         temp = w[i];
                    }else{
                         temp = "["+w[i].toUpperCase()+"|"+w[i].toLowerCase()+"]";
                    }
                    content+=temp;
                }else{
                    if(Character.isDigit(w[i].charAt(0))) {
                        temp = w[i]+"[\\\\s]*";
                    }else{
                        temp = "["+w[i].toUpperCase()+"|"+w[i].toLowerCase()+"]"+"[\\\\s]*";
                    }
                    content+=temp;
                }
            }
            content+=endPrefix;
            content+=shu;
        }

        System.out.println(content.substring(0,content.length()-1));
    }
}



@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
class Dish {
    private String id;
    private String name;
}




